const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const path = require("path");

// 爬取的网站
// 分析页数 1-26页 对应地址 https://www.tu11.com/shoujibizhi/list_41_1.html  -- https://www.tu11.com/shoujibizhi/list_41_26.html
var cateArr = [];
for (let i = 1; i <= 26; i++) {
  let cateUrl = `https://www.tu11.com/shoujibizhi/list_41_${i}.html`;
  cateArr.push(cateUrl);
}
cateArr.forEach((item, index) => {
  getItem(item);
});
// 获取子项地址
async function getItem(url) {
  let res = await axios.get(url);
  let $ = cheerio.load(res.data);
  $(".pic4list .col-xs-1-5 .shupic>a").each((i, item) => {
    let itemUrl = "https://www.tu11.com" + $(item).attr("href");
    getUrl(itemUrl);
  });
}
async function getUrl(url) {
  let res = await axios.get(url);
  let $ = cheerio.load(res.data);
  $(".container-fluid .nry img").each((i, item) => {
    let imgUrl ='https://www.tu11.com/'+$(item).attr("src");
    let time = new Date().getTime();
    let extName = path.extname(imgUrl);
    let ws = fs.createWriteStream(`./img/${time}${extName}`);
    axios.get(imgUrl, { responseType: "stream" }).then((res) => {
      res.data.pipe(ws);
    });
  });
}
